package cc.sunni.test;

import cc.sunni.test.enums.CaloricLevel;
import cc.sunni.test.pojo.Dish;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通过lambda和StreamAPI改善代码可读性
 *
 * @author jiangli
 * @since 2021/5/17 8:53
 */
public class ChangeCode {
    private static List<Dish> menu;

    static {
        menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH));
    }

    public static void main(String[] args) {

        // 在Dish添加getCaloricLevel(),优化按照热量级别进行分类
        Map<CaloricLevel, List<Dish>> collect = menu.stream().collect(Collectors.groupingBy(Dish::getCaloricLevel));

        // 安排热量排序
        List<Dish> collect1 = menu.stream().sorted(Comparator.comparing(Dish::getCalories)).collect(Collectors.toList());

        // 热量求和
        int sum = menu.stream().mapToInt(Dish::getCalories).sum();

        // 使用并行筛选出热量大于300的菜单名
        List<String> collect2 = menu.parallelStream().filter(e -> e.getCalories() > 300).map(Dish::getName).collect(Collectors.toList());


    }
}
